package com.gitee.ykr.lightweight.rpc.util;


import com.caucho.hessian.io.HessianInput;
import com.caucho.hessian.io.HessianOutput;
import com.gitee.ykr.lightweight.rpc.common.exception.CommonException;

import java.io.*;

/**
 * @author YKR
 * @Describe
 * @date 2021/8/8
 */
public class SerializationUtil {

    public static <T> byte[] serialize(T object) {
        if (object == null) {
            throw new CommonException( "Object is null" );
        }

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = null;
        byte[] bytes = null;
        try {
            oos = new ObjectOutputStream( baos );
            oos.writeObject( object );
            bytes = baos.toByteArray();
        } catch (Exception e) {
            throw new CommonException( e.getMessage() );
        } finally {
            try {
                if (oos != null) {
                    oos.close();
                }
            } catch (Exception e) {

            } finally {
                oos = null;
            }

            try {
                if (baos != null) {
                    baos.close();
                }
            } catch (Exception e) {

            } finally {
                baos = null;
            }

        }

        return bytes;
    }

    @SuppressWarnings("unchecked")
    public static <T> T deserialize(byte[] bytes) {
        if (bytes == null || bytes.length == 0) {
            throw new CommonException( "Bytes is null or empty" );
        }

        ByteArrayInputStream bais = new ByteArrayInputStream( bytes );
        ObjectInputStream ois = null;
        Object object = null;
        try {
            ois = new ObjectInputStream( bais );
            object = ois.readObject();
        } catch (Exception e) {
            throw new CommonException( e.getMessage() );
        } finally {
            try {
                if (ois != null) {
                    ois.close();
                }
            } catch (Exception e) {

            } finally {
                ois = null;
            }

            try {
                if (bais != null) {
                    bais.close();
                }
            } catch (Exception e) {

            } finally {
                bais = null;
            }
        }

        return (T) object;
    }




//    private static ByteArrayOutputStream outputStream = null;
//    private static HessianOutput output = null;
//    private static ByteArrayInputStream inputStream = null;
//    private static HessianInput input = null;
//
//    /**
//     * 序列化
//     * @param obj
//     * @param <T>
//     * @return
//     */
//    public static <T> byte[] serialize(T obj) {
//        try {
//            outputStream = new ByteArrayOutputStream();
//            output = new HessianOutput(outputStream);
//            output.writeObject(obj);
//            return outputStream.toByteArray();
//        } catch (Exception e) {
//            e.printStackTrace();
//            throw new RuntimeException("序列化失败！");
//        } finally {
//            try {
//                if (output != null) {
//                    output.close();
//                }
//                if (outputStream!=null){
//                    outputStream.close();
//                }
//            } catch (Exception e) {
//                e.printStackTrace();
//                throw new RuntimeException("关闭资源失败！");
//            }
//        }
//    }
//
//    /**
//     * 反序列化
//     * @param data
//     * @param clazz
//     * @param <T>
//     * @return
//     */
//    public static <T> T deserialize(byte[] data, Class<T> clazz){
//        try {
//            inputStream=new ByteArrayInputStream(data);
//            input=new HessianInput(inputStream);
//            T t = (T)input.readObject(clazz);
//            return t;
//        }catch (Exception e){
//            e.printStackTrace();
//            throw new RuntimeException("反序列化失败！");
//        }finally {
//            try {
//                if (input != null) {
//                    input.close();
//                }
//                if (inputStream!=null){
//                    inputStream.close();
//                }
//            } catch (Exception e) {
//                e.printStackTrace();
//                throw new RuntimeException("关闭资源失败！");
//            }
//        }
//    }
}